//
//  ViewController.m
//  UI05_UIControl
//
//  Created by wyx on 16/3/19.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "imageViewAnimation.h"
#import "mybutton.h"
#import "mySwitch.h"
#import "myslider.h"
#import "segment.h"
#import "pageControl.h"

@interface ViewController ()
@property (nonatomic, retain)imageViewAnimation *imageview;
@property (nonatomic, retain)mySwitch *swith;
@property (nonatomic, retain)myslider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createbutton];
    [self createswitch];
    [self createImageView];
    [self createSlider];
    [self createSegment];
    [self createPageontrol];
}
#pragma mark - Image
- (void)createImageView{
    self.imageview = [[imageViewAnimation alloc]initWithImage:[UIImage imageNamed:@"angry_00.jpg"]];
    self.imageview.frame = CGRectMake(0, 0, 400, 500);
    self.imageview.center = self.view.center;
    [self.view addSubview:self.imageview];
    [_imageview release];
}
#pragma mark - button
- (void)createbutton{
    mybutton *button = [mybutton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(120, 640, 60, 40);
    [self.view addSubview:button];
    [button release];
    [button setTitle:@"开启" forState: UIControlStateNormal];
    [button setTitle:@"关闭" forState: UIControlStateNormal];
    [button addTarget:self action:@selector(handlebutton:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)handlebutton:(mybutton *)button{
    //uibutton 有个 selected 属性(bool 类型)
    button.selected = !button.selected;
    if (button.isSelected) {
        button.backgroundColor = [UIColor redColor];
    }else{
        button.backgroundColor = [UIColor blueColor];
    }
}
#pragma mark - 知识点1: UISwitch
- (void)createswitch{
    self.swith = [[mySwitch alloc] initWithFrame:CGRectMake(50, 650, 0, 0)];
    [self.view addSubview:self.swith];
    [_swith release];
    [self.swith addTarget:self action:@selector(handleswith:) forControlEvents:UIControlEventValueChanged];
}
- (void)handleswith:(mySwitch *)swi{
    NSMutableArray *array = [NSMutableArray array];
    if (swi.on) {
        //创建数组
        for (int i = 0; i < 26; i++) {
            NSString *str = [NSString stringWithFormat:@"angry_%02d",i];
            //创建路径
            NSString *path = [[NSBundle mainBundle]pathForResource:str ofType:@"jpg"];
            //neicun 比 imageName 方法优化
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [array addObject:image];
        }
        self.imageview.animationImages = array;
        self.imageview.animationDuration = 20 - self.slider.value;
        self.imageview.animationRepeatCount = 1;
        [self.imageview startAnimating];
    }
    else{
        [self.imageview stopAnimating];
        self.imageview.animationImages = nil;
        [array removeAllObjects];
    }
}
#pragma mark - 知识点2:UISlider
-(void)createSlider{
    self.slider = [[myslider alloc] initWithFrame:CGRectMake(50, 680, 200, 40)];
    [self.view addSubview:self.slider];
    [_slider release];
    [self.slider addTarget:self action:@selector(handleslider:) forControlEvents:UIControlEventValueChanged];
    //最大值最小值赋值
    self.slider.minimumValue = 1;
    self.slider.maximumValue = 20;
}
-(void)handleslider:(myslider *)slider{
    //动画执行时
    if (self.swith.on){
        self.imageview.animationDuration = 20 - slider.value;
        [self.imageview startAnimating];
    }
}
#pragma mark - 知识点3:UISegmentControl
- (void)createSegment{
    //数组中放入字符串对象 或者 image 对象
    segment *seg = [[segment alloc] initWithItems:@[@"蓝",@"红",@"黄"]];
    seg.frame = CGRectMake(50, 50, 300, 40);
    [self.view addSubview:seg];
    [seg release];
    [seg addTarget:self action:@selector(handleseg:)forControlEvents:UIControlEventValueChanged];
}
-(void)handleseg:(segment *)seg{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor blueColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor yellowColor];
            break;
            
        default:
            break;
    }
}

#pragma mark - 知识点4:UIPageControl
- (void)createPageontrol{
    pageControl *page = [[pageControl alloc] initWithFrame:CGRectMake(20, 450, 80, 40)];
    [self.imageview addSubview:page];
    [page release];
    //设置页数
    //page.center = self.imageview.center
    page.numberOfPages = 5;
    [page addTarget:self action:@selector(handlepage:) forControlEvents:UIControlEventValueChanged];
}
-(void)handlepage:(pageControl *)page{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
